<?php
/*
 *      edinici_izmerenija.inc
 *
 *      Copyright 2010 Rakitin Vitaliy <virak@ukr.net>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

require_once("./classes/GUI/nwftable.php");
require_once("./classes/GUI/nwfeditor.php");
require_once("./classes/DB/nwdataset.php");
require_once("./classes/GUI/nwfform.php");
require_once("./classes/GUI/nwedit.php");


class insertform extends nwfform {

	private $dataset;

	function __construct($uuid, $dataset) {
		parent::__construct($uuid);

		$this->dataset = &$_SESSION[$dataset];
		$out = "Form for insert row";
		$out .= "<br>";
		$out .= "My dataset is: ". $this->dataset->uuid;
		$out .= "<br>";

		$header = $this->dataset->getheader();
		$out .= "<table align='center'>";

		foreach($header as $cell) {
			$out .= "<tr>";
			$id = $cell["id"];
			$name = $cell["name"];
			$out .= "<th style='text-align:right;'>$name</th>";
			$edit = new nwedit($this->dataset->uuid, $id);
			$out .= "<td style='border: solid black 1px;text-align:left;'>". $edit->get() ."</td>";
			$out .= "</tr>";
		}

		$out .= "</table>";
		$out .= "<input type='button' name='close' value='Insert' onclick=message('$this->uuid','insert','',0);>";
		$out .= "<input type='button' name='close' value='Cancel' onclick=message('$this->uuid','cancel','',0);>";
		$this->body = $out;
	}

	function cancel() {
		parent::close();
		$this->dataset->cancel();
	}

	function insert() {
		if ($this->dataset->post()) {
			parent::close();
		}
	}
}

class editform extends nwfform {

	private $dataset;

	function __construct($uuid, $dataset) {
		parent::__construct($uuid);

		$this->dataset = &$_SESSION[$dataset];
		$out = "Form for edit row";
		$out .= "<br>";
		$out .= "My dataset is: ". $this->dataset->uuid;
		$out .= "<br>";

		$header = $this->dataset->getheader();
		$out .= "<table align='center'>";

		foreach($header as $cell) {
			$out .= "<tr>";
			$id = $cell["id"];
			$name = $cell["name"];
			$out .= "<th style='text-align:right;'>$name</th>";
			$edit = new nwedit($this->dataset->uuid, $id);
			$out .= "<td style='border: solid black 1px;text-align:left;'>". $edit->get() ."</td>";
			$out .= "</tr>";
		}

		$out .= "</table>";
		$out .= "<input type='button' name='close' value='Update' onclick=message('$this->uuid','update','',0);>";
		$out .= "<input type='button' name='close' value='Cancel' onclick=message('$this->uuid','cancel','',0);>";
		$this->body = $out;
	}

	function cancel() {
		parent::close();
		$this->dataset->cancel();
	}

	function update() {
		if ($this->dataset->post()) {
			parent::close();
		}
	}
}

class deleteform extends nwfform {

	private $dataset;

	function __construct($uuid, $dataset) {
		parent::__construct($uuid);

		$this->dataset = &$_SESSION[$dataset];
		$out = "Form for delete row";
		$out .= "<br>";
		$out .= "My dataset is: ". $this->dataset->uuid;
		$out .= "<br>";
		$out .= "<br>";
		$out .= "<b>Delete this row?</b>";
		$out .= "<br>";


		$header = $this->dataset->getheader();
		$out .= "<table align='center'>";

		foreach($header as $cell) {
			$out .= "<tr>";
			$id = $cell["id"];
			$name = $cell["name"];
			$out .= "<th style='text-align:right;'>$name</th>";
			$out .= "<td style='border: solid black 1px;text-align:left;'>".$this->dataset->getfieldbyid($id)."</td>";
			$out .= "</tr>";
		}

		$out .= "</table>";

		$out .= "<input type='button' name='delete' value='Delete' onclick=message('$this->uuid','delete','',0);>";
		$out .= "<input type='button' name='cancel' value='Cancel' onclick=message('$this->uuid','cancel','',0);>";

		$this->body = $out;
	}

	function delete() {
		if ($this->dataset->post()) {
			parent::close();
		}
	}

	function cancel() {
		parent::close();
		$this->dataset->cancel();
	}

}


class dataset extends nwdataset {
	function insert() {
		if (parent::insert()) {
			$form = new insertform("uuid2d142feeb65542099a85235dcd0d20d7", $this->uuid);
			$form->show();
		}
	}

	function edit() {
		if (parent::edit()) {
			//$form = new editform("uuid1165bf262a84c0144a3ea4cbef6188c2", $this->uuid);
			//$form->show();
		}
	}

	function cancel() {
		parent::cancel();
	}

	function delete() {
		if (parent::delete()) {
			//$form = new deleteform("uuid2d142feeb65542099a85235dcd0d20d5", $this->uuid);
			//$form->show();
		}
	}
}
?>
